﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ninject;
using Example.Base.Entities;
using Example.Base.Interfaces.Repositories;
using Example.Base.Interfaces.Services;
using Example.Base.Interfaces.Validators;
using Example.Base.Exceptions;

namespace Example.Data.Services
{
    public class RegistrationService: IRegistrationService
    {
        [Inject]
        public IRepository<User> Repository { get; set; }

        [Inject]
        public IValidator<User> Validator { get; set; }

        #region Implementation of IRegistrationService

        public void Registration(User newUser)
        {
            try
            {
                if (Validator.Validate(newUser))
                {
                    Repository.Insert(newUser);

                    if (OnSuccess != null)
                        OnSuccess(newUser, EventArgs.Empty);
                }
                else
                {
                    throw new RegistrationException("Registration failed. Model is invalid.");
                }
            }
            catch (Exception ex)
            {
                if (OnFail != null)
                    OnFail(ex, EventArgs.Empty);
            }
        }

        public event EventHandler OnSuccess;
        public event EventHandler OnFail;

        #endregion
    }
}
